package com.meitianhui.convenitenbuy.module.home.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.home.contract.IntegrationContract;
import com.meitianhui.convenitenbuy.module.home.http.HomeApiDataSource;
import com.meitianhui.convenitenbuy.module.home.ui.activity.IntegrationActivity;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.util.DateUtil;

import java.util.HashMap;
import java.util.Map;

public class IntegrationPresenter implements IntegrationContract.Presenter {

    private IntegrationActivity mContext;
    private IntegrationContract.View mView;

    public IntegrationPresenter(IntegrationContract.View view, IntegrationActivity context) {
        this.mView = view;
        this.mContext = context;
    }

    @Override
    public void payIntegration(String goodId, String activityId) {
        Map<String, String> params = new HashMap<>();
        params.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        params.put("goods_id", goodId);
        params.put("activity_id", activityId);
        String paramJson = new Gson().toJson(params);

        HomeApiDataSource.getInstance()
                .payIntegration(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "gdActivity.consumer.gdActivityDeliveryCountFind", paramJson)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(mContext) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.payIntegrationSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.onFail(code, msg);
                    }
                });
    }

    @Override
    public void userIntegrationPay(String integration, String goodId, String activityId) {
        Map<String, String> params = new HashMap<>();
        params.put("data_source", Constants.CUSTOM_APP);
        params.put("payment_way_key", Constants.PAYMENT_WAY_09);
        params.put("detail", "积分支付");
        params.put("amount", integration);//总金额
        params.put("buyer_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        params.put("seller_id", Constants.SELLER_ID);
        params.put("order_type_key", "DDLX_20");//表示C端
        params.put("transaction_date", DateUtil.getyyyyMMddHHmmss());//表示C端

        Map<String, String> params2 = new HashMap<>();
        params2.put("goods_id", goodId);
        params2.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        params2.put("point", integration);
        params2.put("member_mobile", InfoProvider.getUserToken(App.getInstance()).getMobile());
        params2.put("member_type_key", "consumer");
        params2.put("activity_id", activityId);
        params.put("out_trade_body", new Gson().toJson(params2));
        String paramJson = new Gson().toJson(params);

        HomeApiDataSource.getInstance()
                .userIntegrationPay(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "finance.orderPay", paramJson)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(mContext) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.userIntegrationPaySuccess(response.data);
                    }
                });
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

}
